package Controls
{
	import Config.GameConfig;
	
	import Objects.GameSprite;

	public class NumStepper extends GameSprite
	{
		public var input:TextInput;
		public var reduceButton:Button;
		public var addButton:Button;
		public var maxValue:int=0;
		public var minValue:int=0;
		public var changeSpeed:int=5;
		private var addCharge:int=0;
		private var reduceCharge:int=0;
		public function NumStepper()
		{
			super();
			
			maxValue=GameConfig.maxValue;
			
			input=new TextInput();
			input.field.textField.restrict="0-9";
			addChild(input);
			
			addButton=new Button();
			addChild(addButton);
			
			reduceButton=new Button();
			addChild(reduceButton);
			
			intelligent=true;
		}
		override protected function $update():void
		{
			if(addButton.mHit)
			{
				changeValue(1);
			}
			if(reduceButton.mHit)
			{
				changeValue(-1);
			}
			if(addButton.mDown)
			{
				addCharge++;
				if(addCharge>20&&ticks%changeSpeed==0)
				{
					changeValue(1);
				}
			}
			else
			{
				addCharge=0;
			}
			if(reduceButton.mDown)
			{
				reduceCharge++;
				if(reduceCharge>20&&ticks%changeSpeed==0)
				{
					changeValue(-1);
				}
			}
			else
			{
				reduceCharge=0;
			}
		}
		public function changeValue(step:int):void
		{
			var nextValue:int=int(input.field.textField.text)+step;
			nextValue=Math.min(nextValue,maxValue);
			nextValue=Math.max(nextValue,minValue);
			input.field.textField.text=String(nextValue);
		}
		public function get value():int
		{
			return int(input.field.textField.text);
		}
		public function setup(width:Number,height:Number):void
		{
			var inputWidth:Number=width-reduceButton.obj.width-addButton.obj.width;
			input.setup(inputWidth,height);
			addButton.moveTo(0,input.obj.height/2,0,0.5);
			reduceButton.moveTo(width,input.obj.height/2,1,0.5);
			input.moveTo(width/2,0,0.5,0);
		}
	}
}